<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Parse_model extends AppModel {
    
    var $_tablename = PAGE;
    var $_primary_key = PAGE_PAGE_ID;
    
    function Parse_model() {
        parent::AppModel();
    }
    
    function parse_content($string_content) {
        $CI = & get_instance();
        $delim = array('{', '}');
        $arr_content = array();
        if (preg_match_all('/\{.*\}/', $string_content, $match))
            $arr_content = $match[0];
        $blocks = array();
        for ($i = 0;$i < count($arr_content);$i++) {
            $block_sign = $arr_content[$i];
            $arr_content[$i] = str_replace($delim, "", $arr_content[$i]);
            $arr_temp = split("_", $arr_content[$i]);
            if (count($arr_temp) != 2)
                continue;

            $block_id = $arr_temp[1];
            if (!is_numeric($block_id))
                continue;
//            print_r($block_id);die;
            $block = $CI->load->module_model_factory('home.block_model', $block_id);
            $block->block_sign = $block_sign;
            $blocks[] = $block;
        }
        //print_r($blocks);
        foreach ($blocks as $block) {
            $block->page_id = $CI->page_model->get_id();
            if ($block->is_loaded())
                $s = Modules::run('blocks.' . $block->function, TRUE, $block);
            else
                $s = $arr_content[$i];
            $string_content = str_replace($block->block_sign, $s, $string_content);
        }
		if($CI->page_model->show_flag==0){
			return $s = Modules::run('front.'.$string_content);
		}	
        return $string_content;
    }
}